#!/bin/sh

PDATE=`/bin/date -d 'last day' +%F`
logdb1='mysql -uqunarlog -p2HhxtsSel6rg -h192.168.0.253 logstat --default-character-set=utf8'

if [ $# -eq 1 ];then
       PDATE=$1
fi

resultfile="/export/tmp/train$PDATE.result"
bookingfile="/export/tmp/booking_$PDATE.csv"
pvuvfile="/export/tmp/pvuv_$PDATE.csv"
echo "delete from trainbookinginfo where reportdate='$PDATE';" >$resultfile
cat  /logs/qunarlog/train_srv/l-train*.t.cn5/$PDATE|awk  -v pdate=$PDATE '{if(match($7,/&vid=([^&])*&/,xrr)){arr[xrr[1]]++}}END{for(x in arr){gsub(/\\*'\''/,"\\'\''",x);print "insert into trainbookinginfo(reportdate,codebase,bookingcnt) values(\047"pdate"\047,\047"x"\047,"arr[x]");"}}' >>$resultfile
$logdb1 < $resultfile

$logdb1 -e "select reportdate,codebase,bookingcnt from trainbookinginfo where reportdate<='$PDATE' and reportdate>=date_add('$PDATE',interval -31 day) order by reportdate desc;"|tr "\t" ",">$bookingfile
$logdb1 -e "select thedate,pv,uv from beacon_product_pvuv where product='火车票' and thedate<='$PDATE' and thedate>=date_add('$PDATE',interval -31 day) order by thedate desc;"|tr "\t" ",">$pvuvfile

echo ""|mutt -s "$PDATE火车票booking和pvuv数据" -a $bookingfile -a $pvuvfile taoran.xu@qunar.com
#echo ""|mutt -s "$PDATE火车票booking和pvuv数据" -a $bookingfile -a $pvuvfile taoran.xu@qunar.com,kaisheng@qunar.com -c datar-request@qunar.com,juan.tang@qunar.com,yan.huo@qunar.com,hu.yao@qunar.com
